package com.variometer.daos;

import android.content.Context;
import android.location.Location;
import android.util.Xml;

import com.variometer.models.ActualData;

import org.xmlpull.v1.XmlSerializer;

import java.io.FileOutputStream;
import java.io.StringWriter;


/**
 * Created by michal.kudlacak on 7/14/13.
 */
public class DaoActualData {
    private Context context;
    FileOutputStream outputStream;
    public DaoActualData(Context context) {
        this.context=context;
    }
    public void SaveToFile(String filename,ActualData data) throws Exception
    {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);

        try{
            outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            for(Location location : data.getLocations())
            {
                xmlSerializer.startTag("","Place");
                xmlSerializer.text(String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude())+ "," +
                        String.valueOf(location.getAltitude()));
                xmlSerializer.endTag("","Place");
            }
            outputStream.write(writer.toString().getBytes());
            outputStream.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
